<?php
/**
 * Class that operate on table 'registros'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-11-23 21:12
 */
class RegistrosMySqlDAO implements RegistrosDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return RegistrosMySql 
	 */
	public function load($idDeCompetencia, $idCompetidor, $fase){
		$sql = 'SELECT * FROM registros WHERE id_deCompetencia = ?  AND id_competidor = ?  AND fase = ? ';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($idDeCompetencia);
		$sqlQuery->setNumber($idCompetidor);
		$sqlQuery->setNumber($fase);

		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM registros';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM registros ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param registro primary key
 	 */
	public function delete($idDeCompetencia, $idCompetidor, $fase){
		$sql = 'DELETE FROM registros WHERE id_deCompetencia = ?  AND id_competidor = ?  AND fase = ? ';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($idDeCompetencia);
		$sqlQuery->setNumber($idCompetidor);
		$sqlQuery->setNumber($fase);

		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param RegistrosMySql registro
 	 */
	public function insert($registro){
		$sql = 'INSERT INTO registros (tiempos, puntaje, id_deCompetencia, id_competidor, fase) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($registro->tiempos);
		$sqlQuery->setNumber($registro->puntaje);

		
		$sqlQuery->setNumber($registro->idDeCompetencia);

		$sqlQuery->setNumber($registro->idCompetidor);

		$sqlQuery->setNumber($registro->fase);

		$this->executeInsert($sqlQuery);	
		//$registro->id = $id;
		//return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param RegistrosMySql registro
 	 */
	public function update($registro){
		$sql = 'UPDATE registros SET tiempos = ?, puntaje = ? WHERE id_deCompetencia = ?  AND id_competidor = ?  AND fase = ? ';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($registro->tiempos);
		$sqlQuery->setNumber($registro->puntaje);

		
		$sqlQuery->setNumber($registro->idDeCompetencia);

		$sqlQuery->setNumber($registro->idCompetidor);

		$sqlQuery->setNumber($registro->fase);

		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM registros';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByTiempos($value){
		$sql = 'SELECT * FROM registros WHERE tiempos = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPuntaje($value){
		$sql = 'SELECT * FROM registros WHERE puntaje = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByTiempos($value){
		$sql = 'DELETE FROM registros WHERE tiempos = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPuntaje($value){
		$sql = 'DELETE FROM registros WHERE puntaje = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return RegistrosMySql 
	 */
	protected function readRow($row){
		$registro = new Registro();
		
		$registro->idDeCompetencia = $row['id_deCompetencia'];
		$registro->idCompetidor = $row['id_competidor'];
		$registro->fase = $row['fase'];
		$registro->tiempos = $row['tiempos'];
		$registro->puntaje = $row['puntaje'];

		return $registro;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return RegistrosMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>